package com.example.chat.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Config;
import com.example.chat.beans.Role;
import com.example.chat.mapper.ConfigMapper;
import com.example.chat.mapper.RoleMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.Global;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class ConfigService extends ServiceImpl<ConfigMapper, Config> {
    @Resource
    CheckUtils checkUtils;
    @Resource
    Global global;
    @Resource
    RoleMapper roleMapper;

    public ApiResult<Config> getConfig(Integer userId, String token) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "9901");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Config> result = new ApiResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            Config config = global.getConfig();
            List<Role> roleList = roleMapper.selectByAdminId(userId);
            for (Role role : roleList) {
                if (role.getId() == 2) {
                    config.setShengwangAppId("");
                    config.setAliyunKey("");
                    config.setAliyunId("");
                    config.setAliyunCode("");
                    break;
                }
            }
            result.setData(config);
            status = 1;
            msg = "获取成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> editConfig(Integer userId, String token, String aliyunId, String aliyunKey, String shengwangAppId, double monthPay, double yearPay, double oncePay, int record,
                                      int longRecord, int monthRecordTimes, int yearRecordTimes, int maxSaveTime, String aliyunCode,
                                      String cip,String aliyunSign,Integer pointMultiple, Integer unLoginChatTime) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, aliyunId, aliyunKey, shengwangAppId), userId, token, "9902");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "更新失败";
        try {
            Config config = global.getConfig();
            config.setAliyunId(aliyunId);
            config.setAliyunKey(aliyunKey);
            config.setShengwangAppId(shengwangAppId);
            config.setOncePay(oncePay);
            config.setMonthPay(monthPay);
            config.setYearPay(yearPay);
            config.setRecord(record);
            config.setLongRecord(longRecord);
            config.setMonthRecordTimes(monthRecordTimes);
            config.setYearRecordTimes(yearRecordTimes);
            config.setMaxSaveTime(maxSaveTime);
            config.setAliyunCode(aliyunCode);
            config.setCip(cip);
            config.setAliyunSign(aliyunSign);
            config.setPointMultiple(pointMultiple);
            config.setUnLoginChatTime(unLoginChatTime);
            baseMapper.updateById(config);
            global.setConfig(config);
            status = 1;
            msg = "更新成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<String> getCip() {
        ApiResult<String> result = new ApiResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            Config config = global.getConfig();
            if (config != null) {
                result.setData(config.getCip());
                status = 1;
                msg = "获取成功";
            } else msg = "配置不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
}
